 
var locationclient = require('../../../sys/locationclient')


 
const app = getApp()

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    xtid: { // 1社区团长点，2到店自提点
      type: Number,
      value: 1
    },
    showselected: { // 属性名
      type: Boolean,
      value: true
    },
    selectdefault: { // 属性名
      type: Boolean,
      value: true
    },
    themcolor: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    locationaddrtxt: "定位中...",
    searchkey: "", 
    addrlist: [],   
    CustomBar: app.globalData.CustomBar,
    isshow: false,
  },
  ready: function () {
    
  

    
     //this.locationcitys()
    // this.locationaddrfunc(true)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    Backfunc() {      
      this.setData({ isshow: false })
    },
    show(){
      this.setData({isshow:true})
    this.locationaddrfunc(true)
    },
    locationaddrfunc(rest) {     

      var app = getApp();
      if (app.globalData.locationaddr && app.globalData.locationaddr.status != -1 && !rest) {   
        this.setData({ locationaddr: app.globalData.locationaddr, locationaddrtxt: app.globalData.locationaddr.addrinfo })
      }
      else {
        var that = this;     
        locationclient.locationaddr(null,rest,1).then(function(reslut){
          var updatemodel={};
          
          if(reslut.status==1)
          {
            updatemodel["addrlist"]=reslut["pois"]
            delete reslut["pois"];
          
          }
          updatemodel["locationaddr"]=reslut;
          updatemodel["locationaddrtxt"]= reslut.addrinfo ;
          that.setData(updatemodel)
        })       
      }
    },
    locationcitys(location,rest){
      var that = this
      var config={ 
        get_poi:1,     
        success: function (res) { 
          
          //搜索成功后的回调
        },
        fail: function (res) {
       
        },
        complete: function (res) {
        
        }
      };
      if(location)
      {
        config["location"]=location["lat"] +","+location["lng"]
      }   
   
      this.data.qqmapsdk.reverseGeocoder(config );
    },

    watchinput: function (e) {
      this.data.searchkey = e.detail;
    },
    seachclick: function (e) {
      this.seach(this.data.searchkey)
    },
    seach(keyword) {
      var that = this;  
      locationclient.search(keyword).then(function(reslut){     
        if(reslut["status"]==1){
          that.setData({ addrlist: reslut.data })
        }
        else
        {
           wx.showToast({
             message: reslut["message"],
           })
        } 

      })

    },




 

    //选择当前地址
    selecteditem(e) {
      var dataset = e.currentTarget.dataset;
      var selectaddr = this.data.addrlist[dataset["idx"]];
      var locationaddr = { lat: selectaddr.location.lat, lng: selectaddr.location.lng }
      locationaddr["status"] = 1
      var ad_info = selectaddr["ad_info"];
 
      var fields = ["province", "city", "district", "street"]
      if (ad_info) {
        fields.forEach(item => {
          if (ad_info[item]) {
            locationaddr[item] = ad_info[item]
          }
          else {
            locationaddr[item] = "";
          }

        })
      }
      locationaddr["title"]=selectaddr["title"]
      
      locationaddr["addrinfo"] = selectaddr["address"]
    

      app.globalData.locationaddr =locationaddr;
      this.setData({ locationaddr: locationaddr, locationaddrtxt: locationaddr.addrinfo ,isshow:false})



      this.triggerEvent("change", locationaddr)
    },
    onReachBottom: function (e) {

    },
    scroll: function (e) {

      if (e.detail.scrollTop > 100) {
        this.setData({
          scroll: true
        })
      } else {
        this.setData({
          scroll: false
        })
      }
    },


  }
})